#include<stdio.h>
#include<string.h>
#include<unistd.h>

//开始界面函数
void showWelcomeScreen()
{
    void registerUser();
    void loginUser();
    //打印开始界面
	printf("================================================\n");
	printf("|             欢迎使用图书管理系统             |\n");
	printf("|**********************************************|\n");
	printf("|                    1,登录                    |\n");
	printf("|                    2,注册                    |\n");
	printf("================================================\n\n");
    printf("请选择您将进行的操作(数字):\n");

    //界面的跳转
    int begin;
    scanf("%d",&begin);
    switch (begin)
    {
    case 1:
        loginUser();
        break;
    case 2:
        registerUser();
		break;
    default:
        printf("Warning:输入错误!即将跳转回开始界面!请重新开始!");
        showWelcomeScreen();
        break;
    }
}

//定义学生账户结构体
struct student
{
	char student_name[10];
	char student_ID[11];
	char student_phone_number[12];
	char student_account[13];
	char student_password[17];
}student[10000];

//定义管理员账户结构体
struct admin
{
	char admin_name[10];
	char admin_ID[11];
	char admin_phone_number[12];
	char admin_account[13];
	char admin_password[17];
}admin[100];

//初始化管理员与学生id
int student_id = 0,admin_id = 0;

//账户注册函数
void registerUser()
{
    void student_account_save();
	void admin_account_save();
	//注册用户身份选择界面
	printf("\n");
	printf("================================================\n");
	printf("|                请选择用户身份                |\n");
	printf("|**********************************************|\n");
	printf("|                  1,学生用户                  |\n");
	printf("|                  2,管理员用户                |\n");
	printf("================================================\n");
    printf("请输入您的选择(数字):\n");
	
	int select;
	scanf("%d",&select);
	switch (select)
	{
	case 1:
		//学生注册
		char s_name[10],s_ID[11],s_phone_number[12],s_account[13],s_password_1[17],s_password_2[17];
		printf("\n下面开始注册账户\n\n");
		printf("请输入您的姓名\n");
		scanf("%s",s_name);
		printf("请输入您的学号(10位)\n");
		scanf("%s",s_ID);
		printf("请输入您的联系方式(11位)\n");
		scanf("%s",s_phone_number);
		printf("请输入您的账号(不多于12位)\n");
		scanf("%s",s_account);
		printf("请输入您的密码(不多于16位)\n");
		scanf("%s",s_password_1);
		printf("请再次输入您的密码\n");
		scanf("%s",s_password_2);
		if (strcmp(s_password_1,s_password_2) != 0)
		{
			printf("两次输入的密码不一致!请重新开始注册!\n\n");
			registerUser();
		}
		else
		{
			strcpy(student[student_id].student_name,s_name);
			strcpy(student[student_id].student_ID,s_ID);
			strcpy(student[student_id].student_phone_number,s_phone_number);
			strcpy(student[student_id].student_account,s_account);
			strcpy(student[student_id].student_password,s_password_1);
			student_account_save();
			student_id++;
			printf("学生账户注册成功!欢迎使用图书管理系统!\n\n");
		}
		break;
	case 2:
		//验证身份
		printf("请输入您的密钥(10位):\n");
		char your_key[11];
		scanf("%s",your_key);
		your_key[11] = '\0';
		FILE* keyfile;
		if((keyfile = fopen("key.txt","r")) == NULL)
		{
			printf("文件打开失败,验证未成功,请重新尝试\n");
		}
		char key[20];
		fgets(key,20,keyfile);

		//密钥正确,开始管理员注册
		if(strcmp(your_key,key) == 0)
		{
			char a_name[10],a_ID[11],a_phone_number[12],a_account[13],a_password_1[17],a_password_2[17];
			printf("\n验证成功!下面开始注册账户\n\n");
			printf("请输入您的姓名\n");
			scanf("%s",a_name);
			printf("请输入您的学号或教职工号(10位)\n");
			scanf("%s",a_ID);
			printf("请输入您的联系方式(11位)\n");
			scanf("%s",a_phone_number);
			printf("请输入您的账号(不多于12位)\n");
			scanf("%s",a_account);
			printf("请输入您的密码(不多于16位)\n");
			scanf("%s",a_password_1);
			printf("请再次输入您的密码\n");
			scanf("%s",a_password_2);
			if (strcmp(a_password_1,a_password_2) != 0)
			{
				printf("两次输入的密码不一致!请重新开始注册!\n\n");
				registerUser();
			}
			else
			{
				strcpy(admin[admin_id].admin_name,a_name);
				strcpy(admin[admin_id].admin_ID,a_ID);
				strcpy(admin[admin_id].admin_phone_number,a_phone_number);
				strcpy(admin[admin_id].admin_account,a_account);
				strcpy(admin[admin_id].admin_password,a_password_1);
				admin_account_save();
				admin_id++;
				printf("管理员账户注册成功!欢迎使用图书管理系统!\n\n");
			}
		}
		//密钥错误,返回身份选择界面
		else
		{
			printf("密钥错误,请重新选择身份!\n\n");
			registerUser();
		}
		break;
		
	default:
		//输入了错误的值
		printf("Warning:输入错误!请重新选择身份!\n");
		registerUser();
		break;	
	}
	printf("3秒后将跳转至开始界面\n\n");
	sleep(3);
	showWelcomeScreen();
}

//学生信息保存函数
void student_account_save()
{
	FILE* studentfile;
	if((studentfile = fopen("students_data.txt","a")) == NULL)
	{
		printf("文件打开失败,注册未成功,请重新尝试\n");
		registerUser();
	}
	fputc('{',studentfile);
	fputs(student[student_id].student_name,studentfile);
	fputc(',',studentfile);
	fputs(student[student_id].student_ID,studentfile);
	fputc(',',studentfile);
	fputs(student[student_id].student_phone_number,studentfile);
	fputc(',',studentfile);
	fputs(student[student_id].student_account,studentfile);
	fputc(',',studentfile);
	fputs(student[student_id].student_password,studentfile);
	fputc('}',studentfile);
	fclose(studentfile);
}

//管理员信息保存函数
void admin_account_save()
{
	FILE* adminfile;
	if((adminfile = fopen("admin_data.txt","a")) == NULL)
	{
		printf("文件打开失败,注册未成功,请重新尝试\n");
		registerUser();
	}
	fputc('{',adminfile);
	fputs(admin[admin_id].admin_name,adminfile);
	fputc(',',adminfile);
	fputs(admin[admin_id].admin_ID,adminfile);
	fputc(',',adminfile);
	fputs(admin[admin_id].admin_phone_number,adminfile);
	fputc(',',adminfile);
	fputs(admin[admin_id].admin_account,adminfile);
	fputc(',',adminfile);
	fputs(admin[admin_id].admin_password,adminfile);
	fputc('}',adminfile);
	fclose(adminfile);
}